
 
var AllEnities = []; //储存所有绘制对象

//测量距离
var measureDistance = function (viewer) {
	var isDraw = false;
	var polyline = new Cesium.Entity();
	var polylinePath = [];  //点集合
	var tooltip = document.getElementById("toolTip");
	var LineEntities = [];//所有折现对象
	var disNums = []; //线路长度之和
	var StartPoint;
	var temLine = null;

	var handler = viewer.screenSpaceEventHandler;
	tooltip = document.getElementById("toolTip");
	/***************************鼠标移动事件***********************************/
	handler.setInputAction(function (movement) {
		var position1;
		var cartographic;
		var ray = viewer.scene.camera.getPickRay(movement.endPosition);
		if (ray)
			position1 = viewer.scene.globe.pick(ray, viewer.scene);
		if (position1)
			cartographic = Cesium.Ellipsoid.WGS84.cartesianToCartographic(position1);
		if (cartographic) {
			//海拔
			var height = viewer.scene.globe.getHeight(cartographic);
			//地理坐标（弧度）转经纬度坐标
			var point = Cesium.Cartesian3.fromDegrees(cartographic.longitude / Math.PI * 180, cartographic.latitude / Math.PI * 180, height);
			if (isDraw) {
				tooltip.style.left = movement.endPosition.x + 10 + "px";
				tooltip.style.top = movement.endPosition.y + 20 + "px";
				tooltip.style.display = "block";
				if (polylinePath.length < 1) {
					return;
				}
				if (temLine != null) //清除临时线
				{
					viewer.entities.remove(temLine);
				}
				if (polylinePath.length == 1 && point.x != null) {

					temLine = viewer.entities.add({
						polyline: {
							show: true,
							positions: [polylinePath[0], point],
							material: new Cesium.PolylineOutlineMaterialProperty({
								color: Cesium.Color.RED
							}),
							width: 2
						}
					});
					AllEnities.push(temLine);
					var distance = sum(disNums) + Number(getLineDis(polylinePath[0], point));//自己实现
					tooltip.innerHTML = '<p>长度：' + distance.toFixed(2) + '公里</p><p>双击确定终点</p>';
				}
			}
		}

	}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

	/***************************鼠标移动事件***********************************/


	/***************************鼠标单击事件***********************************/
	//完成画线操作
	handler.setInputAction(function (movement) {
		isDraw = true;
		var position1;
		var cartographic;
		var ray = viewer.scene.camera.getPickRay(movement.position);
		if (ray)
			position1 = viewer.scene.globe.pick(ray, viewer.scene);
		if (position1)
			cartographic = Cesium.Ellipsoid.WGS84.cartesianToCartographic(position1);
		//世界坐标转地理坐标（弧度）
		if (cartographic) {
			//海拔
			var height = viewer.scene.globe.getHeight(cartographic);
			//地理坐标（弧度）转经纬度坐标
			var point = Cesium.Cartesian3.fromDegrees(cartographic.longitude / Math.PI * 180, cartographic.latitude / Math.PI * 180, height);

			polylinePath.push(point); //加点
			if (isDraw && polylinePath.length == 1) {
				StartPoint = point;
				var strartpoint = viewer.entities.add(
				 {
					 position: point,
					 point: {
						 heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
						 show: true,
						 color: Cesium.Color.SKYBLUE,
						 pixelSize: 3,
						 outlineColor: Cesium.Color.YELLOW,
						 outlineWidth: 1
					 },
					 label: {
						 text: "起点",
						 font: '14pt monospace',
						 color: Cesium.Color.RED,
						 backgroundColor: Cesium.Color.CORAL,
						 style: Cesium.LabelStyle.FILL_AND_OUTLINE,
						 outlineWidth: 2,
						 //垂直位置
						 heightReference: Cesium.HeightReference.NONE,
						 verticalOrigin: Cesium.VerticalOrigin.TOP,
						 pixelOffset: new Cesium.Cartesian2(50, 0)
					 }
				 }
			 );
				AllEnities.push(strartpoint);
			}

			if (isDraw && polylinePath.length > 1) {

				var text = 0;
				text = sum(disNums) + Number(getLineDis(polylinePath[0], polylinePath[1]));
				disNums.push(getLineDis(polylinePath[0], polylinePath[1]));
				var temppoint = viewer.entities.add(
					  {
						  position: point,
						  point: {
							  heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
							  show: true,
							  color: Cesium.Color.SKYBLUE,
							  pixelSize: 3,
							  outlineColor: Cesium.Color.YELLOW,
							  outlineWidth: 1
						  },
						  label: {
							  text: text.toFixed(2).toString() + '公里',
							  font: '14pt monospace',
							  color: Cesium.Color.RED,
							  backgroundColor: Cesium.Color.CORAL,
							  style: Cesium.LabelStyle.FILL_AND_OUTLINE,
							  outlineWidth: 2,
							  //垂直位置
							  heightReference: Cesium.HeightReference.NONE,
							  verticalOrigin: Cesium.VerticalOrigin.TOP,
							  pixelOffset: new Cesium.Cartesian2(50, 0)
						  }
					  }
				  );

				AllEnities.push(temppoint);
				polyline = viewer.entities.add({
					polyline: {
						show: true,
						positions: polylinePath,
						material: new Cesium.PolylineOutlineMaterialProperty({
							color: Cesium.Color.RED
						}),
						width: 2
					}
				});
				AllEnities.push(polyline);
				LineEntities.push(polyline); //加直线
				var lastpoint = polylinePath[polylinePath.length - 1];
				polylinePath = [lastpoint];
			}
		}
	}, Cesium.ScreenSpaceEventType.LEFT_CLICK);

	/***************************鼠标单击事件***********************************/

	/***************************鼠标双击事件***********************************/
	handler.setInputAction(function () {
		handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
		handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
		AllEnities.push(polyline);
		viewer.trackedEntity = undefined;
		isDraw = false;
		tooltip.style.display = "none";
		polylinePath = [];
		//LineEntities = [];
		// polyline = null;

	}, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
	/***************************鼠标双击事件***********************************/
}


//获取俩点的距离，返回公里单位值
function getLineDis(startPoint, endPoint) {
	var x2 = (endPoint.x - startPoint.x) * (endPoint.x - startPoint.x)
	var y2 = (endPoint.y - startPoint.y) * (endPoint.y - startPoint.y);
	var dis = Math.sqrt(x2 + y2) / 1000;
	return dis.toFixed(2);
}

function sum(arr) {
	var s = 0;
	for (var i = arr.length - 1; i >= 0; i--) {
		s += Number(arr[i]);
	}
	return s;
}

   //测量面积
var measureArea = function (viewer) {
	var tooltip = document.getElementById("toolTip");
	var isDraw = false;
	var polygonPath = [];
	var polygon = null;
	var handler = viewer.screenSpaceEventHandler;
	handler.setInputAction(function (movement) {

		//新增部分
		var position1;
		var cartographic;
		var ray = viewer.scene.camera.getPickRay(movement.endPosition);
		if (ray)
			position1 = viewer.scene.globe.pick(ray, viewer.scene);
		if (position1)
			cartographic = Cesium.Ellipsoid.WGS84.cartesianToCartographic(position1);
		if (cartographic) {
			//海拔
			var height = viewer.scene.globe.getHeight(cartographic);
			var point = Cesium.Cartesian3.fromDegrees(cartographic.longitude / Math.PI * 180, cartographic.latitude / Math.PI * 180, height);
			if (isDraw) {
				tooltip.style.left = movement.endPosition.x + 10 + "px";
				tooltip.style.top = movement.endPosition.y + 20 + "px";
				tooltip.style.display = "block";

				if (polygonPath.length < 2) {
					return;
				}
				if (!Cesium.defined(polygon)) {
					polygonPath.push(point);
					polygon = new CreatePolygon(polygonPath, Cesium);
					AllEnities.push(polygon);
				} else {
					polygon.path.pop();
					polygon.path.push(point);
					AllEnities.push(polygon);
				}
				if (polygonPath.length >= 2) {
					tooltip.innerHTML = '<p>双击确定终点</p>';
				}
			}
		}

	}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
	handler.setInputAction(function (movement) {
		isDraw = true;
		//新增部分
		var position1;
		var cartographic;
		var ray = viewer.scene.camera.getPickRay(movement.position);
		if (ray)
			position1 = viewer.scene.globe.pick(ray, viewer.scene);
		if (position1)
			cartographic = Cesium.Ellipsoid.WGS84.cartesianToCartographic(position1);
		if (cartographic) {
			//海拔
			var height = viewer.scene.globe.getHeight(cartographic);
			var point = Cesium.Cartesian3.fromDegrees(cartographic.longitude / Math.PI * 180, cartographic.latitude / Math.PI * 180, height);
			if (isDraw) {
				polygonPath.push(point);
				var tmep = viewer.entities.add({
					position: point,
					point: {
						show: true,
						color: Cesium.Color.SKYBLUE,
						pixelSize: 3,
						outlineColor: Cesium.Color.YELLOW,
						outlineWidth: 1
					},
				});

				AllEnities.push(tmep);
			}
		}


	}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
	handler.setInputAction(function () {
		handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
		handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);

		if (polygonPath.length >= 2) {
			var label = String(countAreaInCartesian3(polygon.path));
			label = label.substr(0, label.indexOf(".", 0));
			var text;
			if (label.length < 6)
				text = label + "平方米";
			else {
				label = String(label / 1000000);
				label = label.substr(0, label.indexOf(".", 0) + 3);
				text = label + "平方公里"
			}

			var textArea = text;
			var lastpoint = viewer.entities.add({
				name: '多边形面积',
				position: polygon.path[polygon.path.length - 1],
				point: {
					pixelSize: 5,
					color: Cesium.Color.RED,
					outlineColor: Cesium.Color.WHITE,
					outlineWidth: 2,
					heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
				},
				label: {
					text: textArea,
					font: '18px sans-serif',
					fillColor: Cesium.Color.GOLD,
					style: Cesium.LabelStyle.FILL_AND_OUTLINE,
					outlineWidth: 2,
					verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
					pixelOffset: new Cesium.Cartesian2(20, -40)
				}
			});

			AllEnities.push(lastpoint);

		}

		viewer.trackedEntity = undefined;
		isDraw = false;
		tooltip.style.display = 'none';

	}, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);

	var CreatePolygon = (function () {
		function _(positions, cesium) {
			if (!Cesium.defined(positions)) {
				throw new Cesium.DeveloperError('positions is required!');
			}
			if (positions.length < 3) {
				throw new Cesium.DeveloperError('positions 的长度必须大于等于3');
			}

			this.options = {
				polygon: {
					show: true,
					hierarchy: undefined,
					outline: true,
					outlineColor: Cesium.Color.WHITE,
					outlineWidth: 2,
					material: Cesium.Color.YELLOW.withAlpha(0.4)
				}
			};
			this.path = positions;
			this.hierarchy = positions;
			this._init();
		}

		_.prototype._init = function () {
			var _self = this;
			var _update = function () {
				return _self.hierarchy;
			};
			//实时更新polygon.hierarchy
			this.options.polygon.hierarchy = new Cesium.CallbackProperty(_update, false);
			var oo = viewer.entities.add(this.options);
			AllEnities.push(oo);
		};

		return _;
	})();

	//微元法求面积
	var countAreaInCartesian3 = function (ps) {
		var s = 0;
		for (var i = 0; i < ps.length; i++) {
			var p1 = ps[i];
			var p2;
			if (i < ps.length - 1)
				p2 = ps[i + 1];
			else
				p2 = ps[0];
			s += p1.x * p2.y - p2.x * p1.y;
		}
		return Math.abs(s / 2);
	}
}

